package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.CustomException;
import com.reggie.dao.CategoryDao;
import com.reggie.pojo.Category;
import com.reggie.pojo.Dish;
import com.reggie.pojo.Setmeal;
import com.reggie.service.ICategoryService;
import com.reggie.service.IDishService;
import com.reggie.service.ISetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements ICategoryService {
    @Autowired
    private IDishService dishService;
    @Autowired
    private ISetMealService setMealService;
    @Override
    public void remove(Long id) {

        LambdaQueryWrapper<Dish> categoryIdLqw = new LambdaQueryWrapper<>();
        //菜品分类
        categoryIdLqw.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(categoryIdLqw);
        if (count1 > 0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }


        LambdaQueryWrapper<Setmeal> setMealLqw = new LambdaQueryWrapper<>();
        //套餐分类
        setMealLqw.eq(Setmeal::getCategoryId,id);
        int count2 = setMealService.count(setMealLqw);
        if (count2 > 0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        super.removeById(id);
    }
}
